﻿using System;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace uREST.Core.Extensions
{
    public static class StringExtensions
    {
        public static string[] TrimSplit(this string original)
        {
            return original.TrimSplit(',');
        }

        public static string[] TrimSplit(this string original, char seperator)
        {
            if (string.IsNullOrEmpty(original))
                return new string[0];

            return original.Split(seperator)
                .Select(str => str.Trim().ToLower())
                .Where(str => !string.IsNullOrEmpty(str.Trim()))
                .ToArray();
        }

        public static string Sha1Hash(this string value)
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("String is empty and not hashable.");

            var bytes = Encoding.UTF8.GetBytes(value);
            var computedHash = new SHA1CryptoServiceProvider().ComputeHash(bytes);
            return BitConverter.ToString(computedHash).Replace("-", "").ToLower();
        }
    }
}
